package com.kezesong.leasing.controller.admin;

import com.kezesong.leasing.common.ResponseResult;
import com.kezesong.leasing.controller.BaseController;
import com.kezesong.leasing.controller.request.admin.AddGoodsRAMReqBody;
import com.kezesong.leasing.controller.request.admin.ModifyGoodsRAMReqBody;
import com.kezesong.leasing.entity.GoodsRAMEntity;
import com.kezesong.leasing.service.GoodsRamService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/admin")
public class GoodsRAMManagerController extends BaseController {

    @Autowired
    private GoodsRamService goodsRAMService;

    @PostMapping("/getAllGoodsRAMInfoList.do")
    @ApiOperation(value = "getAllGoodsRAMInfoList", notes = "获取全部商铺内存列表")
    public ResponseResult<List<GoodsRAMEntity>> getAllGoodsRAMInfoList(){
        List<GoodsRAMEntity> goodsRAMList = goodsRAMService.getGoodsRAMList();

        if (null == goodsRAMList || goodsRAMList.size() == 0){
            return buildNoDataResponse();
        }else {
            ResponseResult<List<GoodsRAMEntity>> listResponseResult = buildSucessResponse(goodsRAMList);
            return listResponseResult;
        }

    }

    @GetMapping("/getGoodsRAMInfoListByGoodsId/{goodsId}")
    @ApiOperation(value = "getGoodsRAMInfoListByGoodsId", notes = "根据商品id获取某个商铺的所有内存")
    public ResponseResult<List<GoodsRAMEntity>> getGoodsColorListByGoodsId(@PathVariable("goodsId") int goodsId){
        List<GoodsRAMEntity> goodsRAMList = goodsRAMService.getGoodsRAMList(goodsId);
        if (null == goodsRAMList || goodsRAMList.size() == 0){
            return buildNoDataResponse();
        }else {
            ResponseResult<List<GoodsRAMEntity>> listResponseResult = buildSucessResponse(goodsRAMList);
            return listResponseResult;
        }
    }

    @PostMapping("/addRAMInfo.do")
    @ApiOperation(value = "addRAMInfo", notes = "管理员添加商品内存")
    public ResponseResult<String> addRAMInfo(@RequestBody AddGoodsRAMReqBody reqBody){
        GoodsRAMEntity goodsRAMEntity = new GoodsRAMEntity();
        BeanUtils.copyProperties(reqBody, goodsRAMEntity);
        int rows = goodsRAMService.addGoodsRAM(goodsRAMEntity);
        ResponseResult<String> responseResult = new ResponseResult<>();
        if (rows<=0){
            responseResult  = buildFailedResponse();
        }
        return responseResult;
    }

    @GetMapping("/deleteGoodsRAMInfo/{ramId}")
    @ApiOperation(value = "deleteGoodsRAMInfo", notes = "管理员删除商品内存")
    public ResponseResult<String> deleteGoodsRAM(@PathVariable("ramId") int ramId){
        int rows = goodsRAMService.deleteGoodsRAM(ramId);
        ResponseResult<String> responseResult = new ResponseResult<>();
        if (rows<=0){
            responseResult  = buildFailedResponse();
        }
        return responseResult;
    }

    @PostMapping("/modifyGoodsRAM.do")
    @ApiOperation(value = "modifyGoodsColor", notes = "管理员修改商品内存")
    public ResponseResult<String> modifyGoodsColor(@RequestBody ModifyGoodsRAMReqBody reqBody){
        GoodsRAMEntity goodsRAMEntity = new GoodsRAMEntity();
        BeanUtils.copyProperties(reqBody,goodsRAMEntity);
        int rows = goodsRAMService.modifyGoodsRAM(goodsRAMEntity);
        ResponseResult<String> responseResult = new ResponseResult<>();
        if (rows<=0){
            responseResult  = buildFailedResponse();
        }
        return responseResult;
    }
}
